/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PengajarUpdateDialog.java
 *
 * Created on 27 Agu 14, 18:59:19
 */
package com.ivanbiz.ui;

import com.ivanbiz.dao.MuridDAO;
import com.ivanbiz.dao.impl.MuridDAOImpl;
import com.ivanbiz.model.Bank;
import com.ivanbiz.model.Murid;
import com.ivanbiz.service.JTextFieldLimit;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Shbt Peterpan
 */
public class MuridUpdateDialog extends JDialog {

    Murid murid;
    MuridDAO muridDAO = new MuridDAOImpl();
    Bank bank;
//    String NIM;
    String nama;

    /**
     * Creates new form PengajarUpdateDialog
     *
     * @param mainFrame
     * @param modal
     */
    public MuridUpdateDialog(MainFrame mainFrame, boolean modal) {
        initComponents();
        jLabel2.setVisible(false);
        jTextFieldNIM.setVisible(false);
        jLabel4.setVisible(false);
        jTextFieldTelp.setVisible(false);
        jLabel7.setVisible(false);
        jDateChooserTanggal.setVisible(false);
        jLabel8.setVisible(false);
        jScrollPane1.setVisible(false);
        jTextAreaAlamat.setVisible(false);
    }

    public MuridUpdateDialog(MainFrame mainFrame, boolean modal, Murid murid) {
        initComponents();
        jLabel2.setVisible(false);
        jTextFieldNIM.setVisible(false);
        jLabel4.setVisible(false);
        jTextFieldTelp.setVisible(false);
        jLabel7.setVisible(false);
        jDateChooserTanggal.setVisible(false);
        jLabel8.setVisible(false);
        jScrollPane1.setVisible(false);
        jTextAreaAlamat.setVisible(false);
//        this.NIM = murid.getNIM();
        this.nama = murid.getNama();
        this.murid = murid;
        labelMurid.setText("Ubah Murid");
        jTextFieldNIM.setText(murid.getNIM());
        jTextFieldNama.setText(murid.getNama());
        jTextFieldTelp.setText(murid.getTelp());
        jTextFieldHP.setText(murid.getHandphone());
        jTextFieldEmail.setText(murid.getEmail());
        jTextAreaAlamat.setText(murid.getAlamat());
        jDateChooserTanggal.setDate(murid.getDate());
        bank = murid.getBank();
        textBank.setText(bank.getNama());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelMurid = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNIM = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNama = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldTelp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldHP = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jDateChooserTanggal = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaAlamat = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        textBank = new javax.swing.JTextField();
        buttonDaftariBank = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        buttonSimpan = new javax.swing.JButton();
        buttonBatal = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        labelMurid.setFont(new java.awt.Font("Tahoma", 1, 24));
        labelMurid.setText("Tambah Murid Baru");

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("NIM :");

        jTextFieldNIM.setDocument(new JTextFieldLimit(10));

        jLabel3.setText("Nama :");

        jTextFieldNama.setDocument(new JTextFieldLimit(30));

        jLabel4.setText("Telp :");

        jTextFieldTelp.setDocument(new JTextFieldLimit(20));

        jLabel5.setText("Handphone :");

        jTextFieldHP.setDocument(new JTextFieldLimit(20));

        jLabel6.setText("Email :");

        jTextFieldEmail.setDocument(new JTextFieldLimit(50));

        jLabel7.setText("Tanggal Lahir :");

        jLabel8.setText("Alamat :");

        jTextAreaAlamat.setDocument(new JTextFieldLimit(100));
        jTextAreaAlamat.setRows(5);
        jScrollPane1.setViewportView(jTextAreaAlamat);

        jLabel9.setText("Bank :");

        textBank.setEditable(false);

        buttonDaftariBank.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/pencarian.jpg"))); // NOI18N
        buttonDaftariBank.setToolTipText("Cari Daftar Bank");
        buttonDaftariBank.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDaftariBankActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldNIM, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldNama, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldTelp, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldHP, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jTextFieldEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jDateChooserTanggal, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(textBank, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonDaftariBank))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel9)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(0, 283, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldNIM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonDaftariBank, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textBank))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTelp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldHP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        buttonSimpan.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/simpan.jpg"))); // NOI18N
        buttonSimpan.setText("Simpan");
        buttonSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSimpanActionPerformed(evt);
            }
        });
        jPanel2.add(buttonSimpan);

        buttonBatal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/ui/icon/batal.jpg"))); // NOI18N
        buttonBatal.setText("Batal");
        buttonBatal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBatalActionPerformed(evt);
            }
        });
        jPanel2.add(buttonBatal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelMurid)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelMurid)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-617)/2, 416, 617);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBatalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBatalActionPerformed
        dispose();
}//GEN-LAST:event_buttonBatalActionPerformed

    private void buttonSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSimpanActionPerformed
        if (murid == null) {
            murid = new Murid();
        }
        murid.setNIM(jTextFieldNIM.getText());
        murid.setNama(jTextFieldNama.getText());
        murid.setTelp(jTextFieldTelp.getText());
        murid.setHandphone(jTextFieldHP.getText());
        murid.setEmail(jTextFieldEmail.getText());
        murid.setAlamat(jTextAreaAlamat.getText());
        murid.setDate(jDateChooserTanggal.getDate() == null ? new Date() : jDateChooserTanggal.getDate());
        murid.setBank(bank);
        validate(murid);
}//GEN-LAST:event_buttonSimpanActionPerformed

    private void buttonDaftariBankActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDaftariBankActionPerformed
        String callBank = "callBank";
        BankDialog bankDialog = new BankDialog(callBank);
        bank = bankDialog.getBank();
        textBank.setText(bank == null ? "" : bank.getNama());
    }//GEN-LAST:event_buttonDaftariBankActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBatal;
    private javax.swing.JButton buttonDaftariBank;
    private javax.swing.JButton buttonSimpan;
    private com.toedter.calendar.JDateChooser jDateChooserTanggal;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaAlamat;
    private javax.swing.JTextField jTextFieldEmail;
    private javax.swing.JTextField jTextFieldHP;
    private javax.swing.JTextField jTextFieldNIM;
    private javax.swing.JTextField jTextFieldNama;
    private javax.swing.JTextField jTextFieldTelp;
    private javax.swing.JLabel labelMurid;
    private javax.swing.JTextField textBank;
    // End of variables declaration//GEN-END:variables

    private void validate(Murid murid) {
        if (murid == null) {
            JOptionPane.showMessageDialog(this, "Murid tidak boleh null");
//        } else if (murid.getNIM() == null) {
//            JOptionPane.showMessageDialog(this, "NIM tidak boleh null");
//        } else if (murid.getNIM().trim().isEmpty()) {
//            JOptionPane.showMessageDialog(this, "NIM tidak boleh kosong");
//        } else if (murid.getNama() == null) {
//            JOptionPane.showMessageDialog(this, "Nama tidak boleh null");
        } else if (murid.getNama().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Nama tidak boleh kosong");
        } else if (murid.getBank() == null) {
            JOptionPane.showMessageDialog(this, "Bank tidak boleh kosong");
        } //        else if (murid.getTelp() == null) {
        //            JOptionPane.showMessageDialog(this, "Telp tidak boleh null");
        //        } else if (murid.getTelp().trim().isEmpty()) {
        //            JOptionPane.showMessageDialog(this, "Telp tidak boleh kosong");
        //        } else if (murid.getHandphone() == null) {
        //            JOptionPane.showMessageDialog(this, "HP tidak boleh null");
        //        } else if (murid.getHandphone().trim().isEmpty()) {
        //            JOptionPane.showMessageDialog(this, "HP tidak boleh kosong");
        //        } else if (murid.getEmail() == null) {
        //            JOptionPane.showMessageDialog(this, "Email tidak boleh null");
        //        } else if (murid.getEmail().trim().isEmpty()) {
        //            JOptionPane.showMessageDialog(this, "Email tidak boleh kosong");
        //        } else if (new ServiceHelper().validateEmail(murid.getEmail())) {
        //            JOptionPane.showMessageDialog(this, "Format Email salah");
        //        } else if (murid.getDate() == null) {
        //            JOptionPane.showMessageDialog(this, "Tanggal Lahir tidak boleh null");
        //        } else if (murid.getAlamat() == null) {
        //            JOptionPane.showMessageDialog(this, "Alamat tidak boleh null");
        //        } else if (murid.getAlamat().trim().isEmpty()) {
        //            JOptionPane.showMessageDialog(this, "Alamat tidak boleh kosong");
        //        } 
        else {
            try {
                long a = murid.getId();
                if (a == 0) {
                    if (muridDAO.validateField(Murid.class, "nama", jTextFieldNama.getText()).equals("Data sudah ada")) {
                        JOptionPane.showMessageDialog(this, "nama sudah ada");
                    } else {
                        muridDAO.saveOrUpdate(murid);
                        dispose();
                    }
                } else {
                    if (!nama.equals(jTextFieldNama.getText())) {
                        if (muridDAO.validateField(Murid.class, "nama", jTextFieldNama.getText()).equals("Data sudah ada")) {
                            JOptionPane.showMessageDialog(this, "nama sudah ada");
                        } else {
                            muridDAO.saveOrUpdate(murid);
                            dispose();
                        }
                    } else {
                        muridDAO.saveOrUpdate(murid);
                        dispose();
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(MuridUpdateDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
